<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="sbName">
        <el-input
          v-model="queryParams.sbName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="设备厂家" prop="sbVender">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.sbVender"-->
      <!--          placeholder="请输入设备厂家"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="厂家电话" prop="venderPhone">-->
<!--        <el-input-->
<!--          v-model="queryParams.venderPhone"-->
<!--          placeholder="请输入厂家电话"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!--      <el-form-item label="采购时间" prop="caigouTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.caigouTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择采购时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="出厂日期" prop="chuchangTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.chuchangTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择出厂日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="设备金额" prop="sbAmount">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.sbAmount"-->
      <!--          placeholder="请输入设备金额"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="负责人" prop="principal">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.principal"-->
      <!--          placeholder="请输入设备负责人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
<!--      <el-form-item label="设备类型" prop="sbType">-->
<!--        <el-input-->
<!--          v-model="queryParams.sbType"-->
<!--          placeholder="请输入设备类型"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="设备状态" prop="sbStatus">
        <el-select v-model="queryParams.sbStatus" placeholder="请选择设备状态" clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="2"></el-option>
          <el-option label="报废" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shebei:shebei:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shebei:shebei:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['shebei:shebei:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-hasPermi="['shebei:shebei:remove']"-->
        <!--        >删除-->
        <!--        </el-button>-->
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['shebei:shebei:export']"-->
        <!--        >导出-->
        <!--        </el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shebeiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="设备id" align="center" prop="shebeiId"/>-->
      <el-table-column label="设备名称" align="center" prop="sbName" show-overflow-tooltip/>
      <!--      <el-table-column label="设备厂家" align="center" prop="sbVender"/>-->
      <el-table-column label="厂家电话" align="center" prop="venderPhone" width="180" show-overflow-tooltip/>
      <!--      <el-table-column label="采购时间" align="center" prop="caigouTime" width="150">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.caigouTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="出厂日期" align="center" prop="chuchangTime" width="150">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.chuchangTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="设备金额" align="center" prop="sbAmount" width="80"/>
      <el-table-column label="设备类型" align="center" prop="sbType"/>
      <el-table-column label="设备状态" align="center" prop="sbStatus" width="80">
        <template slot-scope="scope">
          <span>{{
              scope.row.sbStatus == 1 ? "正常" : scope.row.sbStatus == 2 ? "停用" : scope.row.sbStatus == 3 ? "报废" : ""
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备负责人" align="center" prop="principal"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shebei:shebei:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['shebei:shebei:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goWeixiu(scope.row)"
          >维修
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goBaoyang(scope.row)"
          >保养
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="sbName">
              <el-input v-model="form.sbName" placeholder="请输入设备名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="厂家电话" prop="venderPhone">
              <el-input v-model="form.venderPhone" placeholder="请输入厂家电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--        <el-form-item label="设备厂家" prop="sbVender">-->
        <!--          <el-input v-model="form.sbVender" placeholder="请输入设备厂家"/>-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购时间" prop="caigouTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.caigouTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择采购时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂日期" prop="chuchangTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.chuchangTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择出厂日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备金额" prop="sbAmount">
              <el-input v-model="form.sbAmount" placeholder="请输入设备金额"
                        oninput="value=value.replace(/[^\d.]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型" prop="sbType">
              <el-input v-model="form.sbType" placeholder="请输入设备类型"/>
            </el-form-item>
          </el-col>
        </el-row>

        <!--        <el-form-item label="设备负责人" prop="principal">-->
        <!--          <el-input v-model="form.principal" placeholder="请输入设备负责人"/>-->
        <!--        </el-form-item>-->

        <el-form-item label="设备状态" prop="sbStatus">
          <el-select v-model="form.sbStatus" placeholder="请选择设备状态" clearable
                     @keyup.enter.native="handleQuery">
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
            <el-option label="报废" value="3"></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="设备状态" prop="sbStatus">-->
        <!--          <el-input v-model="form.sbStatus" placeholder="请输入设备状态"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listShebei, getShebei, delShebei, addShebei, updateShebei} from "@/api/shebei/shebei";
import {getToken} from "@/utils/auth";

export default {
  name: "Shebei",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      shebeiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        sbName: null,
        sbVender: null,
        venderPhone: null,
        caigouTime: null,
        chuchangTime: null,
        sbAmount: null,
        principal: null,
        sbType: null,
        sbStatus: null
      },
      // 表单参数
      form: {},
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/shebei/shebei/importData"
      },
      // 表单校验
      rules: {

        venderPhone: [
          {required: true, trigger: "blur", message: "请输入厂家电话"},
        ],
        sbName: [
          {required: true, trigger: "blur", message: "请输入设备名称"},
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 下载模板操作 */
    importTemplate() {
      this.download('shebei/shebei/importTemplate', {}, `template_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "设备导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listShebei(this.queryParams).then(response => {
        this.shebeiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shebeiId: null,
        sbName: null,
        sbVender: null,
        venderPhone: null,
        caigouTime: null,
        chuchangTime: null,
        sbAmount: null,
        principal: null,
        sbType: null,
        sbStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shebeiId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shebeiId = row.shebeiId || this.ids
      getShebei(shebeiId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shebeiId != null) {
            updateShebei(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.principal = this.$store.state.user.name
            addShebei(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 跳转设备维修页面
    goWeixiu(row) {
      this.$router.push({path: '/shebei/weixiujilu', query: {shebeiId: row.shebeiId}});
    },
    // 跳转保养页面
    goBaoyang(row) {
      this.$router.push({path: '/shebei/baoyangjilu', query: {shebeiId: row.shebeiId}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shebeiIds = row.shebeiId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + shebeiIds + '"的数据项？').then(function () {
        return delShebei(shebeiIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shebei/shebei/export', {
        ...this.queryParams
      }, `shebei_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
